using System;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.Serialization;
using FilesTubeDAL;

namespace ShowDisplayElementControl
{
    /// <summary>
    /// Delegates select mechanism
    /// </summary>
    /// <param name="sde">ShowDisplayElement object</param>
    public delegate void ShowDisplayElementSelected(ShowDisplayElement sde);

    /// <summary>
    /// Holds displayable information about chosen show and episode
    /// </summary>
    [Serializable]
    public partial class ShowDisplayElement : UserControl, ICloneable, ISerializable
    {
        public event ShowDisplayElementSelected OnShowDisplayElementSelected;

        /// <summary>
        /// Name of the Show
        /// </summary>
        public string ShowName 
        {
            get { return lbShowName.Text; }
            set { lbShowName.Text = value; } 
        }

        /// <summary>
        /// Name to search for. Can differ from ShowName.
        /// </summary>
        public string SearchName 
        {
            get { return _searchName; }
            set { _searchName = value; } 
        }

        /// <summary>
        /// Season number
        /// </summary>
        public string Season 
        {
            get { return _season; }
            set { _season = value; } 
        }

        /// <summary>
        /// Episode number
        /// </summary>
        public string Episode 
        {
            get { return _episode; }
            set { _episode = value; } 
        }

        /// <summary>
        /// Original US airdate
        /// </summary>
        public string AirDate 
        {
            get { return lbAirDate.Text; }
            set { lbAirDate.Text = value; } 
        }

        /// <summary>
        /// Status icon
        /// </summary>
        public Color Icon 
        {
            get { return lbIcon.BackColor; }
            set { lbIcon.BackColor = value; } 
        }

        /// <summary>
        /// Rapidshare results
        /// </summary>
        public RsResult[] RsResults 
        {
            get { return _rsresults; }
            set { _rsresults = value; }
        }

        /// <summary>
        /// Determines if episode is of HD quality (720p)
        /// </summary>
        public bool IsHd 
        {
            get { return _ishd; }
            set { _ishd = value; }
        }

        /// <summary>
        /// Designates if edit form should bypass season & episode validations
        /// </summary>
        public bool FutureEpisode
        {
            get { return _futureEpisode; }
            set { _futureEpisode = value; }
        }

        private bool _selected = false;
        private Color _selectedColor = Color.FromKnownColor(KnownColor.ActiveCaption);
        private string _season;
        private string _episode;
        private RsResult[] _rsresults;
        private bool _ishd;
        private string _searchName;
        private bool _futureEpisode;

        /// <summary>
        /// Parameterless constructor. Do not use.
        /// </summary>
        public ShowDisplayElement()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Regular constructor. Use this one only for manual invokation.
        /// </summary>
        /// <param name="showName">Name of the show</param>
        /// <param name="searchName">Name of the show to search for</param>
        /// <param name="season">Season number</param>
        /// <param name="episode">Episode number</param>
        /// <param name="isHd">Determines whether episode is of HD quality (720p)</param>
        /// <param name="airDate">Original US airdate</param>
        /// <param name="icon">Status icon</param>
        /// <param name="futureEpisode">Determines if edit form should bypass season & episode validations</param>
        public ShowDisplayElement(string showName, string searchName, string season, string episode, bool isHd, string airDate, Color icon, bool futureEpisode)
        {
            InitializeComponent();

            ShowName = showName;
            SearchName = searchName;
            Season = season;
            Episode = episode;
            IsHd = isHd;
            AirDate = airDate;
            Icon = icon;
            FutureEpisode = futureEpisode;

            UpdateSeasonEpisodeLabel();
        }

        /// <summary>
        /// Deserialization constructor. Do not use directly.
        /// </summary>
        /// <param name="info"></param>
        /// <param name="ctxt"></param>
        public ShowDisplayElement(SerializationInfo info, StreamingContext ctxt)
        {
            InitializeComponent();

            ShowName = (string)info.GetValue("ShowName", typeof(string));
            SearchName = (string)info.GetValue("SearchName", typeof(string));
            Season = (string)info.GetValue("Season", typeof(string));
            Episode = (string)info.GetValue("Episode", typeof(string));
            IsHd = (bool)info.GetValue("isHD", typeof(bool));
            AirDate = (string)info.GetValue("AirDate", typeof(string));
            FutureEpisode = (bool) info.GetValue("FutureEpisode", typeof(bool));

            UpdateSeasonEpisodeLabel();
        }

        /// <summary>
        /// Color when selected
        /// </summary>
        public Color SelectedColor
        {
            get { return _selectedColor; }
            set { _selectedColor = value; }
        }

        /// <summary>
        /// Detemines if object selected
        /// </summary>
        public bool Selected
        {
            get { return _selected; }
            set
            {
                _selected = value;

                if (_selected)
                    if (OnShowDisplayElementSelected != null)
                        OnShowDisplayElementSelected(this);
            }
        }

        /// <summary>
        /// IClonable method
        /// </summary>
        /// <returns>ShowDisplayElement object</returns>
        public object Clone()
        {
            var sde = new ShowDisplayElement(lbShowName.Text, _searchName, _season, _episode, _ishd, lbAirDate.Text, lbIcon.BackColor, _futureEpisode)
                          {
                              _rsresults = _rsresults,
                              Tag = Tag
                          };

            return sde;
        }

        /// <summary>
        /// ISerializable method
        /// </summary>
        /// <param name="info"></param>
        /// <param name="context"></param>
        public void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            info.AddValue("ShowName", ShowName);
            info.AddValue("SearchName", SearchName);
            info.AddValue("Season", Season);
            info.AddValue("Episode", Episode);
            info.AddValue("isHD", IsHd);
            info.AddValue("AirDate", AirDate);
            info.AddValue("FutureEpisode", FutureEpisode);
        }

        /// <summary>
        /// Updates the season and episode label from internal variables
        /// </summary>
        public void UpdateSeasonEpisodeLabel()
        {
            lbSeasonEpisode.Text = "Season " + _season + ", Episode " + _episode;
        }
    }
}
